package com.contory.ssn_lgnd.app.title;

import java.util.ArrayList;
import java.util.Random;

import com.contory.ssn_lgnd.app.Assets;
import com.contory.ssn_lgnd.app.KeyFrameAnimation;
import com.contory.ssn_lgnd.app.comp.ViewTestObject;
import com.contory.ssn_lgnd.app.comp.DrawPtr;
import com.contory.ssn_lgnd.app.comp.UpdatePtr;
import com.contory.ssn_lgnd.app.comp.ViewObject;
import com.contory.ssn_lgnd.framework.gl.SpriteBatcher;
import com.contory.ssn_lgnd.framework.gl.TextureRegion;

public class TitleCloudObject extends ViewObject implements DrawPtr, UpdatePtr{
	Random ran = new Random();
	ArrayList<TitleCloudObjectChd> cloudChdList = new ArrayList<TitleCloudObjectChd>();
	public TitleCloudObject(){
		TitleCloudObjectChd chd = new TitleCloudObjectChd();
		chd.initialize(ran, 5, 5.5f);
		cloudChdList.add(chd);
		
		ran.nextInt();
		chd = new TitleCloudObjectChd();
		chd.initialize(ran, 11, 6);
		cloudChdList.add(chd);

		ran.nextInt();
		chd = new TitleCloudObjectChd();
		chd.initialize(ran, 17, 5.8f);
		cloudChdList.add(chd);
		
		ran.nextInt();
		chd = new TitleCloudObjectChd();
		chd.initialize(ran, 7.5f, 7);
		cloudChdList.add(chd);
		
		ran.nextInt();
		chd = new TitleCloudObjectChd();
		chd.initialize(ran, 13.5f, 6.2f);
		cloudChdList.add(chd);
	}
	
	@Override
	public void update(float deltaTime) {
		for (TitleCloudObjectChd chd : cloudChdList) {
			chd.update(deltaTime);
		}
	}

	@Override
	public void draw(SpriteBatcher batcher) {
		for (TitleCloudObjectChd chd : cloudChdList) {
			chd.draw(batcher);
		}
	}
	
	class TitleCloudObjectChd extends ViewObject implements DrawPtr, UpdatePtr{
		float velX;
		float endX;
		Random ran;// = new Random();
		
		final byte state_mov = 0;
		final byte state_fadeOut = 1;
		final byte state_fadeIn = 2;
		
		byte state = 0;
		
		public void initialize(Random ran, float x, float y){
			this.ran = ran;
			tr = Assets.tex_reg_cloud1;
			//subInit();
			this.x = x;
			this.y = y;
			w = Math.max(4, ran.nextInt(6));
			h = w / 4;
			
			velX = ran.nextFloat();
			if(x >= 8.0f)
				velX *= -1;
			
			if(velX > 0)
				velX = Math.max(0.5f, velX);
			else
				velX = Math.min(-0.5f, velX); 
			
			if(velX == 0.0f)
				velX = 2.0f;
			
			if(velX > 0)
				endX = x + Math.max(ran.nextInt(8), 5);
			else if(velX < 0)
				endX = x - Math.max(ran.nextInt(8), 5);
			
			r = 1.0f;
			g = 0.788f;
			b = 0.054f;
		}
		
		public void subInit(){
			x = Math.max(5.0f, ran.nextFloat() * 17.0f);
			y = Math.max(5.5f, ran.nextFloat() * 7.0f);				
			w = Math.max(4, ran.nextInt(6));
			h = w / 4;
			
			velX = ran.nextFloat();
			if(x >= 8.0f)
				velX *= -1;
			
			if(velX > 0)
				velX = Math.max(0.5f, velX);
			else
				velX = Math.min(-0.5f, velX); 
			
			if(velX == 0.0f)
				velX = 2.0f;
			
			if(velX > 0)
				endX = x + Math.max(ran.nextInt(8), 5);
			else if(velX < 0)
				endX = x - Math.max(ran.nextInt(8), 5);
			
			r = 1.0f;
			g = 0.788f;
			b = 0.054f;
		}
		
		@Override
		public void update(float deltaTime) {
			switch(state){
				case state_mov :
					if(KeyFrameAnimation.translateX(this, deltaTime, velX, endX)){
						state = state_fadeOut;
					}
				break;
				
				case state_fadeOut :
					a -= deltaTime;
					if(a < 0.0f){
						a = 0.0f;
						state = state_fadeIn;
						subInit();
					}
				break;
				
				case state_fadeIn :
					a += deltaTime;
					if(a > 1.0f){
						a = 1.0f;
						state = state_mov;
					}
				break;
			}
		}

		@Override
		public void draw(SpriteBatcher batcher) {
			batcher.drawSprite(x, y, w, h, r, g, b, a, tr);
		}
	}
}
